<?php

/**
 * Controller_cadre
 * 班主任管理-班干部任命
 * 
 */

class Controller_cadre extends MyApp {

	public function __construct()
	{
		parent::__construct();

		$this->model->staff  	 = YOU::loadModel('staff');
		$this->model->class   	 = YOU::loadModel('class');
		$this->model->student    = YOU::loadModel('student');
		$this->model->divide     = YOU::loadModel('divide');
		$this->model->message    = YOU::loadModel('message');
		$this->model->appoint    = YOU::loadModel('appoint');

	}

	/** 首页 **/
	public function actionIndex()
	{

	}

	public function actionList()
	{
		// 权限
		$user = $this->user;
		// 老师有哪些班级
		$appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint` WHERE staff_id='{$user['type_id']}' AND appoint_type = '4'");
		// 获得班级的名称
		foreach ($appoint_lists as $key => $value){
			// 班级名称
			$class_data = $this->model->class->getinfo($value['class_id']);
			$appoint_lists[$key]['class_name'] = $class_data['class_name'];
		}
		// 获得班级的id
		$class_id   = (int)$this->get->class_id;
		if(empty($class_id)){
			foreach ($appoint_lists as $key => $value){
				$class_id = $appoint_lists[0]['class_id'];
			}
		}
		// 查找班级中所有的学生
		$divide_lists = $this->model->student->getClassAll($class_id);
		foreach ($divide_lists as $key => $value){
			// 学生的姓名，学号
			$student_data = $this->model->student->getinfo($value['student_id']);
			$divide_lists[$key]['student_no']  =$student_data['student_no'];
			$divide_lists[$key]['student_name']= $student_data['student_name'];
			// 班级名称
			$class_data = $this->model->class->getinfo($value['class_id']);
			$divide_lists[$key]['class_name'] = $class_data['class_name'];
			// 班干部名称
			$leader_data = $this->model->appoint->getRow($value['leader_id']);
			$divide_lists[$key]['leader_name'] = $leader_data['leader_name'];

		}
		// 班干部全部的名称
		$leader_lists = $this->model->student->getAppAll();
		
		if ($_POST){

			$student_id 	  	  = $this->post->student_id;
			$leader_id 	  		  = $this->post->leader_id;
			$message_type = 1;
			// 读取评语表，把职位也插入这个评语表里
			$divide_datas = $this->model->divide->getInfo($divide_id);

			$info = array(

			"leader_id"  			=> $leader_id,
			);
			$a= $this->db->do_update("tbl_student", $info, "student_id='{$student_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=cadre.list&class_id={$class_id}");

		}

		$this->tpl->assign("divide_lists", $divide_lists);
		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->assign("leader_lists", $leader_lists);
		$this->tpl->assign("class_id", $class_id);
		
		$this->tpl->display("cadre/m.cadre.list.tpl.php");

	}

}

?>